package comjavaday02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class My1 extends JPanel implements Runnable, KeyListener {
    private int x=5;
    private int y=5;

    private boolean up=false;
    private boolean down=false;
    private boolean left=false;
    private boolean right=false;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.setBackground(Color.white);
        for (int i=0;i<1;i++){
         g.setColor(Color.BLACK);

         g.drawOval(x+350,y+80,300,260);
         g.drawOval(x+400,y+140,70,40);
        g.drawOval(x+540,y+140,70,40);
        g.fillRect(x+422,y+141,28,39);
        g.fillRect(x+561,y+141,28,39);
        g.drawLine(x+528,y+248,x+478,y+248);
        g.drawOval(x+480,y+200,50,90);
        g.setColor(Color.white);
        g.fillRect(x+480,y+198,80,50);
        g.fillRect(x+400,y+290,200,195);
        g.setColor(Color.black);
        g.drawLine(x+400,y+305,x+400,y+350);
        g.drawLine(x+600,y+305,x+600,y+350);
        g.setColor(Color.pink);
        g.drawLine(x+390,y+235,x+410,y+215);
        g.drawLine(x+410,y+235,x+430,y+215);
        g.drawLine(x+430,y+235,x+450,y+215);
        g.drawLine(x+550,y+235,x+570,y+215);
        g.drawLine(x+570,y+235,x+590,y+215);
        g.drawLine(x+590,y+235,x+610,y+215);
    }}

    @Override
    public void run() {
        while (true){
            if(up){
                y=y-10;

            }
            if(down){
                y=y+10;
            }if(left){
                x=x-10;
            }if(right){
                x=x+10;
            }

            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            repaint();
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()){
    case KeyEvent.VK_UP:up=true;break;
    case KeyEvent.VK_DOWN:down=true;break;
    case KeyEvent.VK_LEFT:left=true;break;
    case KeyEvent.VK_RIGHT:right=true;break;
}
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()){
            case KeyEvent.VK_UP:up=false;break;
            case KeyEvent.VK_DOWN:down=false;break;
            case KeyEvent.VK_LEFT:left=false;break;
            case KeyEvent.VK_RIGHT:right=false;break;
        }
    }
}
